package cn.com.taiji.pos.schedul;

import cn.com.taiji.pos.common.util.HttpUtil;
import cn.com.taiji.pos.entity.MenuFoodCategory;
import cn.com.taiji.pos.entity.MenuFoodItem;
import cn.com.taiji.pos.entity.MenuFoodPrice;
import cn.com.taiji.pos.service.IMenuFoodCategoryService;
import cn.com.taiji.pos.service.IMenuFoodItemService;
import cn.com.taiji.pos.service.IMenuFoodPriceService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.time.LocalDateTime;
import java.util.List;

//@Component
@Slf4j
public class CommissaryFoodShedul {

    @Value("${GET_COMMISSARY_FOOD_URL}")
    private String GET_COMMISSARY_FOOD_URL;
    @Value("${FOOD_CATEGORY_ID_CODE_xmb}")
    private String FOOD_CATEGORY_ID_CODE_xmb;
    @Value("${COMMISSARY_BUSINESS_TYPE_ID}")
    private String COMMISSARY_BUSINESS_TYPE_ID;

    @Autowired
    private IMenuFoodCategoryService menuFoodCategoryService;
    @Autowired
    private IMenuFoodItemService menuFoodItemService;
    @Autowired
    private IMenuFoodPriceService menuFoodPriceService;

    //    @Scheduled(cron = "0 0 23 ? * SUN")
//    @Scheduled(cron = "0 0 23 L * ?")
    private void synchronizeFood() {
        String result = HttpUtil.sendToPost(GET_COMMISSARY_FOOD_URL);
        String itemSetObject = JSONObject.parseObject(result, JSONObject.class).getString("itemSet");
        MenuFoodCategory menuFoodCategory = menuFoodCategoryService.query().eq("CODE", FOOD_CATEGORY_ID_CODE_xmb).one();
        if (itemSetObject != null) {
            List<MenuFoodItem> menuFoodItemList = JSONObject.parseArray(itemSetObject, MenuFoodItem.class);
            for (MenuFoodItem menuFoodItem : menuFoodItemList) {
                if (StringUtils.isEmpty(menuFoodItem.getBarCode())) {
                    continue;
                }
                MenuFoodItem code = menuFoodItemService.query().eq("CODE", menuFoodItem.getCode()).one();
                if (code != null) {
                    MenuFoodPrice foodPrice = menuFoodPriceService.query().eq("FOOD_ID", code.getId()).one();
                    foodPrice.setPrice(menuFoodItem.getSalePrice());
                    foodPrice.setEndTime(foodPrice.getEndTime().plusMonths(1));
                    menuFoodPriceService.updateById(foodPrice);
                } else {
                    menuFoodItem.setFoodCategoryId(menuFoodCategory.getId());
                    menuFoodItem.setMeasurementId("ec9b1ad1d13b475482c44f110327be0d");
                    menuFoodItem.setStatus("0");
                    menuFoodItem.setCreatorId("admin");
                    menuFoodItem.setCreatorName("系统管理员");
                    menuFoodItemService.save(menuFoodItem);
                    MenuFoodPrice foodPrice = new MenuFoodPrice();
                    foodPrice.setFoodId(menuFoodItem.getId());
                    foodPrice.setBusinessTypeId(COMMISSARY_BUSINESS_TYPE_ID);
                    foodPrice.setPrice(menuFoodItem.getSalePrice());
                    foodPrice.setStartTime(LocalDateTime.now());
                    foodPrice.setEndTime(LocalDateTime.now().plusMonths(1));
                    foodPrice.setLevel(0);
                    foodPrice.setCreateTime(LocalDateTime.now());
                    //全天餐次
                    foodPrice.setMealTimeId("5a38f3931d9f4b05987d744cafccd80a");
                    foodPrice.setPriceVersion(System.currentTimeMillis());
                    foodPrice.setStatus("0");
                    foodPrice.setCreatorId("admin");
                    foodPrice.setCreatorName("系统管理员");
                    menuFoodPriceService.save(foodPrice);
                }
            }
        }
    }
}
